'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 装备
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');

exports.getEquipUpgradePropertyConf = function (equip_type) {
    var equip_type_conf = _getEquipTypeConf(equip_type);
    if (!equip_type_conf) {
        console.error('equip_type_conf type can not found:' + equip_type);
        return 0;
    }

    var equip_quality_conf = _getEquipQualityConf(equip_type_conf.quality);
    if (!equip_quality_conf) {
        return 0;
    }

    return equip_quality_conf.upgrade_prop_id;
};

/**
 * 获取装备合成配置
 * @param  {[type]} equip_type [description]
 * @param  {[type]} equip_lv   [description]
 * @return {[type]}            [description]
 */
exports.getComposeEquipConf = function (equip_quality) {
    return _getComposeEquipConf(equip_quality);
};

/**
 * 获取装备的类型配置
 * @param  {[type]} equip_type [description]
 * @param  {[type]} equip_lv   [description]
 * @return {[type]}            [description]
 */
exports.getEquipTypeConf = function (equip_type) {
    return _getEquipTypeConf(equip_type);
};

/**
 * 获取装备品质配置
 * @param  {[type]} equip_type [description]
 * @return {[type]}            [description]
 */
exports.getEquipQualityConf = function (equip_quality) {
    return _getEquipQualityConf(equip_quality);
};

/**
 * 获取装备的强化等级配置
 * @param  {[type]} equip_type [description]
 * @param  {[type]} equip_lv   [description]
 * @return {[type]}            [description]
 */
exports.getEquipStrengthenLvConf = function (equip_lv) {
    return dataApi.equipment_strengthen.findById(equip_lv);
};

/**
 * 获取装备的等级配置
 * @param  {[type]} equip_lv   [description]
 * @return {[type]}            [description]
 */
exports.getEquipLvConf = function (equip_lv) {
    return dataApi.equipment_lvl.findById(equip_lv);
};

/**
 * 根据装备的类型和等级获取出售的价格
 * @param  {[type]} equip_type [description]
 * @param  {[type]} lv         [description]
 * @return {[type]}            [description]
 */
exports.getEquipSellGold = function (equip_type, equip_lv, refine_lv) {
    var equipment_conf = dataApi.equipment_type.findById(equip_type);
    if (!equipment_conf) {
        console.error('equip type can not found:' + equip_type);
        return 0;
    }
    var sell_gold = equipment_conf.gold;
    var equip_lv_conf = dataApi.equipment_strengthen.findById(equip_lv);
    if (!!equip_lv_conf) {
        sell_gold += equip_lv_conf.restore_gold;
    }
    var refine_lv_conf = dataApi.equipment_lvl.findById(refine_lv);
    if (!!refine_lv_conf) {
        sell_gold += refine_lv_conf.all_cost;
    }
    return sell_gold;
};

/**
 * 装备继承的钻石消耗
 * @param  {[type]} equip_type [description]
 * @param  {[type]} equip_lv   [description]
 * @return {[type]}            [description]
 */
exports.getEquipInheritDiamond = function (equip_type) {
    var equip_type_conf = _getEquipTypeConf(equip_type);
    if (!equip_type_conf) {
        console.error('equip_type_conf type can not found:' + equip_type);
        return 0;
    }
    var equip_quality_conf = _getEquipQualityConf(equip_type_conf.quality);
    if (!equip_quality_conf) {
        console.error('equip_type_conf type can not found:' + equip_type);
        return 0;
    }
    return equip_quality_conf.inherit_point;
};

/**
 * 获取随机合成装备
 * @param  {[type]} compose_id [description]
 * @return {[type]}            [description]
 */
exports.getRandomEquip = function (compose_id) {
    var equip_random_conf = dataApi.goods_random.findById(compose_id);
    if (!equip_random_conf) {
        console.error('compose id can not found:' + compose_id);
        return 0;
    }
    return equip_random_conf;
};

exports.getAdvConf = function (equip_type) {
    return dataApi.equipment_advanced.findById(equip_type);
};

/**
 * 获取随机锻造装备
 * @param  {[type]} forge_lvl     [description]
 * @param  {[type]} forge_part    [description]
 * @param  {[type]} forge_quality [description]
 * @return {[type]}               [description]
 */
exports.getRandomForgeEquip = function (job_type, forge_quality, forge_part) {
    var forge_equip_list = [];
    dataApi.equipment_type.each(function (equip_item, key) {
        if (job_type == equip_item.profession &&
            forge_quality == equip_item.quality &&
            forge_part == equip_item.part) {
            forge_equip_list.push(equip_item.id);
        }
    });
    return forge_equip_list;
};

/**
 * 获取装备类型配置
 * @param  {[type]} equip_type [description]
 * @return {[type]}            [description]
 */
var _getEquipTypeConf = function (equip_type) {
    return dataApi.equipment_type.findById(equip_type);
};

/**
 * 获取装备品质配置
 * @param  {[type]} equip_quality [description]
 * @return {[type]}               [description]
 */
var _getEquipQualityConf = function (equip_quality) {
    return dataApi.quality_related.findById(equip_quality);
};

/**
 * 获取合成装备配置
 * @param  {[type]} equip_quality [description]
 * @return {[type]}               [description]
 */
var _getComposeEquipConf = function (equip_quality) {
    return dataApi.equipment_compose.findById(equip_quality);
};
